import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import {getToken} from "@/utils/auth";

export const constantRoutes: Array<RouteRecordRaw> = [
    {//用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示
        path: '/main',
        name: 'main',
        component: () => import('../components/HomeView.vue'),
        // redirect: '/ADMIN',
        children: [
            {
                name: "Admin",
                path: "/ADMIN",
                component: () => import('../components/Admin_College.vue'),
                meta: {
                    title: "学院管理",
                    icon: "Collection",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"ADMIN"
                },
                children: []
            },

            {
                name: "Event",
                path: "/Event",
                component: () => import('../components/Admin_Event.vue'),
                meta: {
                    title: "赛事管理",
                    icon: "Collection",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"ADMIN"
                },
                children: []
            },
            {
                name: "Admin_Athlete",
                path: "/Admin_Athlete",
                component: () => import('../components/Admin_Athlete.vue'),
                meta: {
                    title: "运动员管理",
                    icon: "Collection",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"ADMIN"
                },
                children: []
            },
            {
                name: "Judge",
                path: "/JUDGE",
                component: () => import('../components/Judge_Result.vue'),
                meta: {
                    title: "成绩查看",
                    icon: "EditPen",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"JUDGE"
                },
                children: []
            },
            {
                name: "Judge_College",
                path: "/Judge_College",
                component: () => import('../components/Judge_College.vue'),
                meta: {
                    title: "学院查看",
                    icon: "EditPen",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"JUDGE"
                },
                children: []
            },
            {
                name: "Athlete",
                path: "/ATHLETE",
                component: () => import('../components/Athlete_College.vue'),
                meta: {
                    title: "学院成绩",
                    icon: "Postcard",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"ATHLETE"
                },
                children: []
            },
            {
                name: "Athlete_Event",
                path: "/Athlete_Event",
                component: () => import('../components/Athlete_Event.vue'),
                meta: {
                    title: "个人成绩",
                    icon: "Postcard",
                    requiresAuth: true, // 路由拦截，未登录不能使用
                    role:"ATHLETE"
                },
                children: []
            }
        ]
    },
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../components/login.vue'),
        children: []
    },
    {
        path: '/:catchAll',
        name: '/404',
        component: () => import('../components/404.vue'),
        meta: {
            title: "404",
            requiresAuth: true, // 路由拦截，未登录不能使用
        },
        children: []
    },

]


export const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    linkActiveClass: 'router-checked',
    routes: constantRoutes
})


router.beforeEach((to, from, next) => {
    window.scrollTo(0, 0);
    // console.log("title",document.title)
    // 通过requiresAuth判断当前路由导航是否需要登录
    if (to.matched.some(record => record.meta.requiresAuth)) {
        let token = getToken()
        // console.log("token",token)
        // 若需要登录访问，检查是否为登录状态
        if (!token) {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        } else {
            next()
        }
    } else {
        next() // 确保一定要调用 next()
    }
})


export default router
